from django import forms
from django.forms import widgets
from django.core.exceptions import ValidationError
import re
from .models import Department


class DepartmentModelForm(forms.ModelForm):
    class Meta:
        model = Department
        # 不显示exclude中指定的字段
        exclude = ['createDate']
        # 定义标签
        labels = {
            'name': '部门名称',
            'leader': '负 责 人:',
            'phone': '手 机 号:',
            'region': '所在区域:',
            'address': '办公位置:',
            'longitude': '区域经度:',
            'latitude': '区域纬度:',
        }

        choice = (("集团总部", "集团总部"), ("华南区域", "华南区域"),
                  ("华北区域", "华北区域"), ("华东区域", "华东区域"),)
        # 定义html控件
        widgets = {
            'name': widgets.TextInput(attrs={"placeholder": "请输入部门名称", "class": "form-control"}),
            'leader': widgets.TextInput(attrs={"placeholder": "请输入负责人", "class": "form-control"}),
            'phone': widgets.NumberInput(attrs={"placeholder": "请输入手机号", "class": "form-control"}),
            'region': widgets.Select(choices=choice, attrs={"class": "form-control"}),
            'address': widgets.TextInput(attrs={"placeholder": "办公所在位置", "class": "form-control"}),
            'longitude': widgets.NumberInput(attrs={"placeholder": "区域所在经度", "class": "form-control"}),
            'latitude': widgets.NumberInput(attrs={"placeholder": "区域所在纬度", "class": "form-control"}),
        }

        # 定义错误提示信息
        error_messages = {
            "name": {
                'required': '部门名称不能为空，请输入!',
                'max_length': "部门名称太长了!"},
            'leader': {
                'required': '负责人不能为空，请输入!',
                'max_length': '负责人名字太长了!'},
            'phone': {
                'required': '手机号不能为空，请输入!'},
        }

    # 手机号验证
    def clean_phone(self):
        phone = str(self.cleaned_data.get('phone'))
        mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
        if not mobile_re.match(phone):
            raise ValidationError('请输入正确的手机号!')
        return phone

# class DepartmentForm(forms.Form):
#     name = forms.CharField(
#         label='部门名称:',
#         required=True,
#         max_length=120,
#         error_messages={
#             'required': '部门名称不能为空，请输入!',
#             'max_length': '部门名称太长了!'},
#         widget=widgets.TextInput(attrs={"placeholder":
#                                             "请输入部门名称", "class": "form-control"}))
#
#     leader = forms.CharField(
#         label='负 责 人:',
#         required=True,
#         max_length=60,
#         error_messages={
#             'required': '负责人不能为空，请输入!',
#             'max_length': '负责人名字太长了!'},
#         widget=widgets.TextInput(attrs={"placeholder":
#                                             "请输入负责人", "class": "form-control"}))
#
#     phone = forms.CharField(
#         label='手 机 号:',
#         required=True,
#         error_messages={
#             'required': '手机号不能为空，请输入!'},
#         widget=widgets.NumberInput(attrs={"placeholder":
#                                               "请输入手机号", "class": "form-control"}))
#
#     choice = (("集团总部", "集团总部"), ("华南区域", "华南区域"),
#               ("华北区域", "华北区域"), ("华东区域", "华东区域"),)
#     region = forms.CharField(
#         label='所在区域:',
#         initial='集团总部',
#         widget=widgets.Select(choices=choice,
#                               attrs={"class": "form-control"}))
#
#     address = forms.CharField(
#         required=False,
#         label='办公位置:',
#         max_length=200,
#         widget=widgets.TextInput(attrs={"placeholder": "办公所在位置",
#                                         "class": "form-control"}))
#
#     longitude = forms.DecimalField(
#         required=False,
#         label='区域经度:',
#         widget=widgets.NumberInput(attrs={"placeholder": "区域所在经度",
#                                           "class": "form-control"}))
#
#     latitude = forms.DecimalField(
#         required=False,
#         label='区域纬度:',
#         widget=widgets.NumberInput(attrs={"placeholder": "区域所在纬度",
#                                           "class": "form-control"}))
#
#     # 手机号验证
#     def clean_phone(self):
#         phone = self.cleaned_data.get('phone')
#         mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]| 18[0 - 9] | 14[57])[0 - 9]{8}$')
#         if not mobile_re.match(phone):
#             raise ValidationError('请输入正确的手机号!')
#         return phone
